<?php

namespace App\Http\Controllers;

use App\Models\HouseAudit;
use App\Models\HouseTypeInfo;
use App\Services\OrderService;
use App\Services\OssQiNiuYun;
use App\Services\RedisGeo;
use App\Services\StatusNumber;
use App\Tools\JsonTools;
use App\Tools\JwtTools;
use Godruoyi\Snowflake\Snowflake;
use Illuminate\Http\Request;

class HouseTypeController extends Controller
{
    /**
     * @Notes:
     * @Author:Planc,
     * @Date: 2022/11/11,
     * @Time: 14:26,
     * 获取房源类型
     */
    public function houseType(){
        $data=HouseTypeInfo::get()->toArray();
        return JsonTools::JsonSuccess($data,200,'ok');
    }

    /**
     * @Notes:
     * @Author:Planc,
     * @Date: 2022/11/11,
     * @Time: 15:00,
     * 上传图片到七牛云
     */
    public function imageInfo(Request $request){
        $image=$request->file('image');
        $resImage=OssQiNiuYun::ossQny($image);
        return JsonTools::JsonSuccess($resImage,200,'ok');
    }

    /**
     * @Notes:
     * @Author:Planc,
     * @Date: 2022/11/11,
     * @Time: 15:00,
     * 七牛云删除图片
     */
    public function imageInfoDelete(Request $request){
        $key=$request->get('file_name');
        $key=str_replace('http://shx.zxh666.top/','',$key);
        $res=OssQiNiuYun::ossQnyDelete($key);
        if($res){
            return JsonTools::JsonSuccess([],200,'ok');
        }

    }

    /**
     * @Notes:
     * @Author:Planc,
     * @Date: 2022/11/11,
     * @Time: 18:54,
     * 发布房源信息
     */
    public function pushHouse(Request $request){
        $uid = $request->id;
        $data=$request->all();

        $resHousePush=HouseAudit::create([
            'house_logo'=>$data['image'],
            'house_name'=>$data['houseName'],
            'house_type'=>$data['houseType'],
            'house_city'=>$data['region'][0],
            'house_area'=>$data['region'][2],
            'house_molding'=>$data['houseMolding'],
            'house_floor'=>$data['houseFloor'],
            'house_acreage'=>$data['houseAcreage'],
            'house_price'=>$data['housePrice'],
            'house_title'=>$data['houseTitle'],
            'house_text'=>$data['houseText'],
            'audit_status'=>0,
            'square_price'=>$data['squarePrice'],
            'house_way'=>$data['houseWay'],
            'house_address'=>$data['houseAddress'],
            'uid'=>$uid,
            'house_number'=>(new Snowflake())->id(),
            'house_latitude'=>$data['latitude'],
            'house_longitude'=>$data['longitude']
        ]);
        if($resHousePush){
            $resOrder=OrderService::createOrder($uid,$data['housePrice'],$data['houseWay'],$resHousePush['id']);
            if($resOrder==StatusNumber::SUCCESS){
                return JsonTools::JsonSuccess($resHousePush['id'],200,'ok');
            }else{
                return  JsonTools::JsonError([],10001,'订单创建失败');
            }
        }else{
            return  JsonTools::JsonError([],10001,'发布失败');
        }
    }

    /**
     * @Notes:
     * @Author:Planc,
     * @Date: 2022/11/11,
     * @Time: 18:54,
     * 发布房源信息
     */
    public function pushRentingHouse(Request $request){
        $uid = $request->id;
        $data=$request->all();
        (new RedisGeo())->setHouseLocation($data['region'][1],$data['region'][2],$data['houseName']);
        $resHousePush=HouseAudit::create([
            'house_logo'=>$data['image'],
            'house_name'=>$data['houseName'],
            'house_type'=>$data['houseType'],
            'house_city'=>$data['region'][0],
            'house_area'=>$data['region'][2],
            'house_molding'=>$data['houseMolding'],
            'house_price'=>$data['housePrice'],
            'house_title'=>$data['houseTitle'],
            'house_text'=>$data['houseText'],
            'audit_status'=>0,
            'house_way'=>$data['houseWay'],
            'house_address'=>$data['houseAddress'],
            'uid'=>$uid,
            'house_number'=>(new Snowflake())->id(),
            'house_latitude'=>$data['latitude'],
            'house_longitude'=>$data['longitude']
        ]);
        if($resHousePush){
            $resOrder=OrderService::createOrder($uid,$data['housePrice'],$data['houseWay'],$resHousePush['id']);
            if($resOrder==StatusNumber::SUCCESS){
                return JsonTools::JsonSuccess($resHousePush['id'],200,'ok');
            }else{
                return  JsonTools::JsonError([],10001,'订单创建失败');
            }
        }else{
            return  JsonTools::JsonError([],10001,'发布失败');
        }
    }
}
